module StarFrame
  module Loadable
    def load dir, object_class = self, postfixes = class_variable_get(:@@postfixes)
      ::Dir.foreach(dir) do |file|
        next if file[0, 1] == "."
        path = "#{dir}/#{file}"
        if File.directory? path
          class_name = file.camelize
          module_eval <<-DEFINE_MODULE, __FILE__, __LINE__
            module #{class_name}
              extend StarFrame::Loadable
              load("#{path}", #{object_class}, *%w{#{postfixes.join " "}})
            end
          DEFINE_MODULE
        else
          next unless postfixes.empty? || postfixes.include?(File.extname(file))
          basename = File.basename(file, ".*")
          class_name = basename.camelize
          const_set class_name, object_class.loaded(path)
        end
      end
    end
    
    def loaded; end
    
    def self.extended klass
      klass.__send__ :class_variable_set, :@@postfixes, []
    end
  end
end

#from dispatcher (scene loader)
=begin
    def initialize base_dir = "."
      @base_dir  = File.expand_path(base_dir)
      @scene_dir = "#{@base_dir}/scenes"
      
      @scenes = {}
      Dir.foreach(@scene_dir) do |scene_filename|
        next if scene_filename == "." || scene_filename == ".."
        scene_name = /^(.*)_scene.rb$/.match(scene_filename)[1] rescue next
        class_name = "#{StarFrame.camelize(scene_name)}Scene"
        @scenes[scene_name.to_sym] = ["#{@scene_dir}/#{scene_filename}", class_name.to_sym]
      end
    end
    
    def scene name = :main
      scene_path, class_name = @scenes[name]
      require scene_path
      Object.const_get(class_name).new
    end
=end